package com.epfresh.bean;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class Category {
    private Object modifiedTime;
    private Object forSupplier;
    private Object forPurchaser;
    private Object creatorId;
    private Object modifierId;
    private String name;
    private Object createdTime;
    private String id;
    private String sort;
    private List<ProductCategory> categories;
    private int status;

    private boolean isSelect;


    //散货
    private double count;
    private List<ProductCategory> item;
    private int childPosition;
    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getChildPosition() {
        return childPosition;
    }

    public void setChildPosition(int childPosition) {
        this.childPosition = childPosition;
    }


    public String getSecondCategoryId() {
        if (id == null || "".equals(id) || type == null || "".equals(type)) {
            return null;
        }
        return id + "|" + type;
    }

    public void updateChildSelectItem() {
        if (item != null) {
            for (int i = 0; i < item.size(); i++) {
                ProductCategory child = item.get(i);
                if (childPosition == i) {
                    child.setSelect(true);
                } else {
                    child.setSelect(false);
                }
            }
        }
    }


    public List<ProductCategory> getItem() {
        return item;
    }

    public void setItem(List<ProductCategory> item) {
        this.item = item;
    }

    public double getCount() {
        return count;
    }

    public void setCount(double count) {
        this.count = count;
    }




    public boolean isSelect() {
        return isSelect;
    }

    public void setSelect(boolean select) {
        isSelect = select;
    }

    public Object getModifiedTime() {
        return this.modifiedTime;
    }

    public void setModifiedTime(Object modifiedTime) {
        this.modifiedTime = modifiedTime;
    }

    public Object getForSupplier() {
        return this.forSupplier;
    }

    public void setForSupplier(Object forSupplier) {
        this.forSupplier = forSupplier;
    }

    public Object getForPurchaser() {
        return this.forPurchaser;
    }

    public void setForPurchaser(Object forPurchaser) {
        this.forPurchaser = forPurchaser;
    }

    public Object getCreatorId() {
        return this.creatorId;
    }

    public void setCreatorId(Object creatorId) {
        this.creatorId = creatorId;
    }

    public Object getModifierId() {
        return this.modifierId;
    }

    public void setModifierId(Object modifierId) {
        this.modifierId = modifierId;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getCreatedTime() {
        return this.createdTime;
    }

    public void setCreatedTime(Object createdTime) {
        this.createdTime = createdTime;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<ProductCategory> getCategories() {
        return categories == null ? item :categories;
    }

    public void setCategories(List<ProductCategory> categories) {
        this.categories = categories;
        this.item = categories;
    }

    public int getStatus() {
        return this.status;
    }

    public void setStatus(int status) {
        this.status = status;
    }


    public List<ProductCategoryNames> getNames() {

        List<ProductCategoryNames> names = new ArrayList<>();
        if (categories != null && categories.size() > 0) {

            for (int i = 0; i < categories.size(); i++) {
                ProductCategory pc = categories.get(i);

                List<ProductCategoryNames> pNames = pc.getItem();
                if (pNames != null && pNames.size() > 0) {
                    ProductCategoryNames pn = new ProductCategoryNames();
                    pn.setName(pc.getName());
                    pn.setId(pc.getId());
                    pn.setTitle(true);
                    names.add(pn);

                    ProductCategoryNames pcn = null;
                    for (int j = 0; j < pNames.size(); j++) {
                        if (j % 3 == 0) {
                            pcn = new ProductCategoryNames();
                            names.add(pcn);
                        }
                        ProductCategoryNames productCategoryNames = pNames.get(j);
                        if(pcn!=null)
                            pcn.addNames(productCategoryNames);

                    }

                }


            }
        }

        return names;
    }
    public List<ProductCategoryNames> getCategoryNames() {

        List<ProductCategoryNames> names = new ArrayList<>();
        if (categories != null && categories.size() > 0) {

            for (int i = 0; i < categories.size(); i++) {
                ProductCategory pc = categories.get(i);

                List<ProductCategoryNames> pNames = pc.getItem();
                if (pNames != null && pNames.size() > 0) {
                    ProductCategoryNames pn = new ProductCategoryNames();
                    pn.setName(pc.getName());
                    pn.setCategoryId(id);
                    pn.setId(pc.getId());
                    pn.setTitle(true);
                    names.add(pn);


                    int n = pNames.size();

                    for (int j = 0; j < n; j++) {

                        ProductCategoryNames categoryNames = pNames.get(j);
                        categoryNames.setCategoryId(id);
                        categoryNames.setProductCategoryId(pc.getId());
                        names.add(categoryNames);


                    }

                    n = n % 2;
                    if (n != 0) {
                        n = 2 - n;
                    }
                    for (int j = 0; j < n; j++) {
                        ProductCategoryNames categoryNames = new ProductCategoryNames();
                        categoryNames.setEmpty(true);

                        names.add(categoryNames);
                    }

                }


            }
        }


        return names;
    }

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }
}
